package com.daluzy.chmanager.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.daluzy.chmanager.MyApplication;
import com.daluzy.chmanager.R;
import com.daluzy.chmanager.bean.Comment;
import com.daluzy.chmanager.bean.Record;
import com.daluzy.chmanager.bean.TestPoint;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 评论
 */
public class AddCommentActivity extends AppCompatActivity {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.radio_good)
    RadioButton radioGood;
    @BindView(R.id.radio_common)
    RadioButton radioCommon;
    @BindView(R.id.radio_low)
    RadioButton radioLow;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
    @BindView(R.id.et_record_details)
    EditText etRecordDetails;
    @BindView(R.id.tv_add_commit)
    TextView tvAddCommit;
    private String recordTitle;
    private String recordDetails;
    private String recordDRemark;

    private LoadingDialog loadingDialog;
    private Record record;
    private String commentLevel;
    private String commentDetails;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_comment_daluzy);
        ButterKnife.bind(this);
        record = (Record) getIntent().getSerializableExtra("record");
        radioGroup.check(R.id.radio_good);
        commentLevel = "好评";
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.radio_good) {
                    commentLevel = "好评";
                } else if (i == R.id.radio_common) {
                    commentLevel = "一般";
                } else {
                    commentLevel = "差评";
                }
            }
        });
        loadingDialog = new LoadingDialog(this);
    }

    @OnClick({R.id.iv_back, R.id.tv_add_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_add_commit:
                tvAddCommit.setEnabled(false);
                check();
                break;
        }
    }

    private void check() {
        commentDetails = etRecordDetails.getText().toString().trim();

        if (TextUtils.isEmpty(commentDetails)) {
            Toast.makeText(AddCommentActivity.this, "请输入评价内容", Toast.LENGTH_SHORT).show();
            tvAddCommit.setEnabled(true);
            return;
        }

        addComment();
    }


    private void addComment() {
        Comment comment = new Comment();
        comment.setComment_level(commentLevel);
        comment.setComment_details(commentDetails);
        comment.setComment_record(record.getObjectId());
        comment.setComment_employee(record.getRecord_employee());
        comment.setComment_status("1");
        comment.setComment_student(record.getRecord_student());


        comment.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                if (e == null) {
                    updateRecord();
                    Toast.makeText(AddCommentActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(AddCommentActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                if (loadingDialog != null)
                    loadingDialog.close();
                tvAddCommit.setEnabled(true);
            }
        });
    }

    private void updateRecord() {
        if (loadingDialog != null)
            loadingDialog.show();
        //更新Person表里面id为6b6c11c537的数据，address内容更新为“北京朝阳”
        record.setRecord_type("3");
        record.update(record.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    finish();
                }
                if (loadingDialog != null)
                    loadingDialog.close();
            }

        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }
}
